package com.guslegend.component;

import com.guslegend.config.ShardingDBConfig;
import com.guslegend.config.ShardingTableConfig;
import com.guslegend.utils.CommonUtils;
import org.springframework.stereotype.Component;

@Component
public class ShortLinkComponent {

    //62个字符
    private static final String CHARS="0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";

    /**
     * 生成短链码
     * @param param
     * @return
     */
    public String createShortLinkCode(String param){

        long murmurHash= CommonUtils.murmurHash32(param);
        //进制转换
        String code=encodeToBase62(murmurHash);
        String shortLinkCode= ShardingDBConfig.getRandomDBPrefix(code)+code+ ShardingTableConfig.getRandomTableSuffix(code);

        return shortLinkCode;
    }


    /**
     * 10进制转62进制
     * @param num
     * @return
     */
    private static String encodeToBase62(long num){

        //多线程里面  StringBuffer线程安全   StringBuilder线程不安全
        StringBuffer sb=new StringBuffer();
        do {
            int i = (int) (num % 62);
            sb.append(CHARS.charAt(i));
            num=num/62;
        }while (num>0);
        String value = sb.reverse().toString();
        return value;
    }
}
